/*
    @author:张润泽
    @Date:2022-04-20
 */
var mySwiper = new Swiper('.mySwiper1', {
    slidesPerView: 1,
    spaceBetween: 10,
    
    pagination:{
        el:'.swiper-pagination',
        clickable:true,
      },
    loop: true,
    autoplay: {
        delay: 3500,
        stopOnLastSlide: false,
        disableOnInteraction: true,
    },
    effect: 'fade',
    fadeEffect: {
        crossFade: true,
    }

})
var ul = document.querySelector('.item_wrap ul');
var next = document.querySelector('.next');
var prev = document.querySelector('.prev');
var itemWrap = document.querySelector('.item_wrap');
var count = 0;
next.onclick = function () {
    if (count == ul.children.length - 5) {
        count = 0;
        ul.style.left = 0;
    }
    count++;
    var target = count * -238;
    retardance(ul, target)
};
prev.addEventListener('click', function () {
    if (count == 0) {
        count = ul.children.length - 5;
        ul.style.left = count * -238 + 'px';
    }
    count--;
    var target = count * -238;
    retardance(ul, target)
})
$('.item_bot').hover(function () {
    clearInterval(timer)
}, function () {
    autoplay();
})
var timer;

function autoplay() {
    timer = setInterval(function () {
        next.onclick();
    }, 2000);
}
autoplay();

function retardance(ele, max) {
    clearInterval(ele.move);
    ele.move = setInterval(() => {
        if (ele.offsetLeft == max) {
            clearInterval(ele.move);
        } else {
            var v = (max - ele.offsetLeft) / 10;
            v = v > 0 ? Math.ceil(v) : Math.floor(v);
            ele.style.left = ele.offsetLeft + v + 'px';
        }
    }, 10);
}






// 读取数据渲染页面
var indexData = data.indexData;

var swiperData = indexData.haohuo;
$.each(swiperData, function (index, value) {
    $(`
    <li>
        <a href="javascript:;">
            <img src="${value.goodurls.nowurl}"
            width="210px" alt="">
            <p class="hot_name">${value.goodtitle}</p>
            <p class="hot_price">¥${value.goodprice}<s>¥${value.goodprice}</s></p>
        </a>
    </li>`).appendTo($('.item_wrap ul'));
})
// 添加无缝效果图
$.each(swiperData, function (index, value) {
    if (index < 5) {
        $(`
            <li>
                <a href="javascript:;">
                    <img src="${value.goodurls.nowurl}"
                    width="210px" alt="">
                    <p class="hot_name">${value.goodtitle}</p>
                    <p class="hot_price">¥${value.goodprice}<s>¥${value.goodprice}</s></p>
                </a>
            </li>`).appendTo($('.item_wrap ul'));
    }

})


var shopList = indexData.mainpic;


$.each(shopList, function (index, value) {
    $(`
                <li>
                    <a href="./details.html?${value.goodid}" data-gid='${value.goodid}' class='godetail'>
                        <img src="${value.goodurls.nowurl}"
                            width="210px" alt="">
                        <p class="hot_name">${value.goodtitle}</p>
                        <p class="hot_price">¥${value.goodzprice}<s>¥${value.goodprice}</s></p>
                    </a>
                </li>
    `).appendTo('.hot ul')
})




// 楼层导航

$(window).scroll(function () {
    // 侧边栏显示
    if ($(window).scrollTop() >= $('.index_festival').offset().top) {
        $('.floor').fadeIn(600);
    } else {
        $('.floor').fadeOut(600);
    };
    // 使侧边栏变色

    $('.lc').each(function (index, value) {
        if ($(window).scrollTop() >= value.offsetTop) {
            $('.floor div').eq(index).addClass('active').children('i').css('display', 'block').siblings().css('color', '#fff').end().end().siblings().removeClass('active').children('i').css('display', 'none').siblings().css('color', '#555');
        }
    })


})
$('.floor div:not(.back_top)').click(function () {
    var index = $(this).index();
    $('html').animate({
        scrollTop: $('.lc').eq(index).offset().top
    }, 500);
})

$('.back_top').click(function () {
    $('html').animate({
        scrollTop: 0
    }, 500);
})













$('.item_wrap ul li').hover(function () {
    $(this).css({
        "transform": "translateY(-3px)"
    }).find('img').css({
        opacity: .7
    })
}, function () {
    $(this).css({
        "transform": "translateY(0px)"
    }).find('img').css({
        opacity: 1
    })
})

$('.warper_top').find('a').hover(function () {
    $(this).css({
        "transform": "translateY(-3px)",
        'box-shadow': '1px 0px 5px 0px black'
    })
}, function () {
    $(this).css({
        "transform": "translateY(0px)",
        'box-shadow': '0px 0px 0px 0px black'
    })
})

$('.warper_bot').find('a').hover(function () {
    $(this).css({
        "transform": "translateY(-3px)",
        'box-shadow': '0px 1px 5px 0px black'
    })
}, function () {
    $(this).css({
        "transform": "translateY(0px)",
        'box-shadow': '0px 0px 0px 0px black'
    })
})


$('.index_bot a').hover(function () {
    $(this).children('img').css({
        "transform": "translateY(-3px)",
        'box-shadow': '0px 0px 5px 0px black'
    })
}, function () {
    $(this).children('img').css({
        "transform": "translateY(0px)",
        'box-shadow': '0px 0px 0px 0px black'
    })
})


$('.p_img').hover(function () {
    $(this).find('img').css({
        "transform": "translateY(-3px)",
        "transform": "scale(1.1)"
    })
}, function () {
    $(this).find('img').css({
        "transform": "translateY(0px)",
        "transform": "scale(1)"
    })
})

$('.four_list img,.three_list img').hover(function () {
    $(this).css({
        // "transform": "translateY(-3px)",
        "transform": "scale(1.1)"
    })
}, function () {
    $(this).css({
        // "transform": "translateY(0px)",
        "transform": "scale(1)"
    })
})


$('.hot li').hover(function () {
    $(this).css({
        "border-color": 'red'
    }).find('img').css({
        'opacity': '0.5'
    }).end().find('.hot_name').css('color', 'red');
}, function () {
    $(this).css({
        "border-color": '#d2d2d2'
    }).find('img').css({
        'opacity': '1'
    }).end().find('.hot_name').css('color', 'black');
})


$('.col_img').hover(function () {
    $(this).css({
        "transform": " translateX(-5px)",

    })
}, function () {
    $(this).css({
        "transform": "translateX(0px)"
    })
})

$('.cont_img').hover(function () {
    $(this).css({
        "transform": "scale(1.05)"
    })
}, function () {
    $(this).css({
        "transform": "scale(1)"
    })
})






$('.vip_tit a').mouseover(function () {
    $(this).addClass('vip_on').siblings().removeClass('vip_on');
    $('.vip_con ul').eq($(this).index()).fadeIn().siblings().css('display', 'none');
});


$(window).scroll(function () {
    if ($(window).scrollTop() > 100) {
        $('.sk_nav').stop().animate({
            top: 0
        }, 150)
    } else {
        $('.sk_nav').stop().animate({
            top: -60
        }, 150)
    }
})






console.log(localStorage.getItem('mobile_phone'))
if (localStorage.getItem('mobile_phone')) {
    $('.toux').attr('src', '../images/index/avatar-a.png')

    $('.nl').css('display', 'none');
    $('.lyes').css('display', 'block')
    $('.txt_info').children('a').css('display', 'none');
    $('.txt_info').children('span').css('display', 'block').end().find('.out').css('display', 'block');
}

$('.gologin').hover(function () {
    $(this).css({
        'background': 'red',
        color: '#fff'
    })
}, function () {
    $(this).css({
        'background': '#fff',
        color: 'red'
    })
})







//   跳转详情页




$('.godetail').click(function () {
    goDetails($(this));
})

function goDetails(ele) {
    var gid = ele.data('gid')
    shopList.forEach(function (value) {
        if (value.goodid == gid) {


            // 将点击的商品添加到浏览记录中
            if (localStorage.getItem('looked')) {
                var look = JSON.parse(localStorage.getItem('looked'));
                var flag = true;
                for (var i in look) {
                    if (look[i].goodid == value.goodid) {
                        flag = false;
                    }
                }
                if (flag) {
                    look.push(value);
                    localStorage.setItem('looked', JSON.stringify(look));
                }
            } else {
                localStorage.setItem('looked', JSON.stringify([value]));
            }

        }
    })
}







// 退出登录
$('.out').click(function () {
    localStorage.removeItem('mobile_phone');
    localStorage.removeItem('password');
    location.assign('../page/login.html');
})



$('.gwc').click(function(){
    location.assign('../page/cart.html')
})